<?php
/*--------------------------------------------------------------------
 小微OA系统 - 让工作更轻松快乐

 Copyright (c) 2013 http://www.smeoa.com All rights reserved.

 Author:  jinzhu.yin<smeoa@qq.com>

 Support: https://git.oschina.net/smeoa/xiaowei
 --------------------------------------------------------------*/

namespace Home\Controller;

class DutyController extends HomeController {
    protected $config = array('app_type' => 'common', 'write' => 'user,get_duty_list,set_duty');

    public function index() {
        $auth = $this -> config['auth'];
        $this -> assign('auth', $auth);

        $list = M("Duty") -> order('sort asc') -> select();
        $this -> assign('list', $list);
        $this -> display();
    }

    function _search_filter(&$map) {
        $map['is_del'] = array('eq', '0');
        $keyword = I('keyword');
        if (!empty($keyword)) {
            $map['User.name|emp_no|Position.name|Dept.name'] = array('like', "%" . $keyword . "%");
        }
    }

    public function del() {
        $role_id = $_POST['id'];
        $where['role_id'] = $role_id;
        $model = M("RoleDuty");
        $model -> where($where) -> delete();
        $this -> _destory($role_id);
    }

    public function user() {
        $map = $this -> _search();
        $auth = $this -> config['auth'];
        $where_duty['is_del'] = array('eq', 0);
        if ($auth['admin']) {
        } elseif ($auth['write']) {
            $sub_dept = get_sub_dept();
            $map['dept_id'] = array('in', $sub_dept);
            $where['user_id'] = array('eq', get_user_id());

            $duty_list = M('DutyUser') -> where($where) -> getField('duty_id', true);
            if (!empty($duty_list)) {
                $where_duty['id'] = array('in', $duty_list);
            } else {
                $where_duty['_string'] = '1=2';
            }
        }

        if (method_exists($this, '_search_filter')) {
            $this -> _search_filter($map);
        }
        $user_list = D("UserView") -> where($map) -> select();
        $this -> assign("user_list", $user_list);

        $duty_list = M("Duty") -> where($where_duty) -> order('sort asc') -> select();
        $this -> assign("duty_list", $duty_list);
        $this -> display();
    }

    public function get_duty_list() {
        $id = I('id');
        $data = D("Duty") -> get_duty_list($id);
        if ($data !== false) {// 读取成功
            $return['data'] = $data;
            $return['status'] = 1;
            $this -> ajaxReturn($return);
        }
    }

    public function set_duty() {
        $user_list = $_POST["user_id"];
        $duty_list = $_POST["duty_list"];

        $model = D("Duty");
        $model -> del_duty($user_list);

        $result = $model -> set_duty($user_list, $duty_list);
        if ($result === false) {
            $this -> error('操作失败！');
        } else {
            $this -> assign('jumpUrl', get_return_url());
            $this -> success('操作成功！');
        }
    }

}
?>